# 一定要在settings文件中将异常模块配置自己的异常处理函数
from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework.response import Response
from rest_framework import status
from .logging import logger
def exception_handler(exc, context):
    # 自定义的异常处理
    # exc: 本次请求发送的异常信息
    # context: 本次请求发送异常的执行上下文【本次请求的request对象，本次请求的request请求体，异常发送的时间，行号等....】
    response = drf_exception_handler(exc, context)

    detail = '%s - %s - %s' % (context.get('view'), context.get('request').method, exc)
    if not response:  # 服务端错误
        response =  Response({'detail': detail}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    else:
        response.data = {'detail': detail}

    # 核心：要将response.data.get('detail')信息记录到日志文件
    logger.critical(response.data.get('detail'))

    return response

